/*
 * AddEventDialog.java
 *
 * Created on 29-Mar-2010, 21:26:23
 */
package gui;

import backend.Core;
import backend.Plugin;
import backend.RegexPlugin;
import backend.Server;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.swing.DefaultListModel;

/**
 *
 * @author Khazidhea
 */
public class EditEventDialog extends javax.swing.JDialog {

    private MainFrame parent;
    private Core core;
    private DefaultListModel serverListModel;
    private DefaultListModel logListModel = new DefaultListModel();
    private Plugin event;

    /** Creates new form AddEventDialog */
    public EditEventDialog(MainFrame parent) {
        initComponents();
        this.parent = parent;
        this.core = parent.getCore();
        this.serverListModel = parent.getServerListModel();

        serverList.setModel(serverListModel);
        logList.setModel(logListModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addPanel = new javax.swing.JPanel();
        addEventTabPanel = new javax.swing.JTabbedPane();
        addSimpleEventPanel = new javax.swing.JPanel();
        regexTextField = new javax.swing.JTextField();
        regexLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        serverList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        logList = new javax.swing.JList();
        serverListLabel = new javax.swing.JLabel();
        logListLabel = new javax.swing.JLabel();
        saveChangesButton = new javax.swing.JButton();
        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        addComplexEventPanel = new javax.swing.JPanel();

        regexLabel.setText("Regex:");

        serverList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                serverListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(serverList);

        logList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                logListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(logList);

        serverListLabel.setText("Server List:");

        logListLabel.setText("Log List");

        saveChangesButton.setText("Save Changes");
        saveChangesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveChangesButtonActionPerformed(evt);
            }
        });

        nameLabel.setText("Name:");

        javax.swing.GroupLayout addSimpleEventPanelLayout = new javax.swing.GroupLayout(addSimpleEventPanel);
        addSimpleEventPanel.setLayout(addSimpleEventPanelLayout);
        addSimpleEventPanelLayout.setHorizontalGroup(
            addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addSimpleEventPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(addSimpleEventPanelLayout.createSequentialGroup()
                        .addGroup(addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(serverListLabel)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 97, Short.MAX_VALUE)
                        .addGroup(addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(logListLabel)
                            .addGroup(addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(saveChangesButton)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(21, Short.MAX_VALUE))
                    .addGroup(addSimpleEventPanelLayout.createSequentialGroup()
                        .addGroup(addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(regexLabel)
                            .addComponent(nameLabel))
                        .addGap(18, 18, 18)
                        .addGroup(addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(nameTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 679, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(regexTextField, javax.swing.GroupLayout.Alignment.LEADING))
                        .addContainerGap())))
        );
        addSimpleEventPanelLayout.setVerticalGroup(
            addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addSimpleEventPanelLayout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .addGroup(addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(regexLabel)
                    .addComponent(regexTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverListLabel)
                    .addComponent(logListLabel))
                .addGap(18, 18, 18)
                .addGroup(addSimpleEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(saveChangesButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12))
        );

        addEventTabPanel.addTab("Simple Event", addSimpleEventPanel);

        javax.swing.GroupLayout addComplexEventPanelLayout = new javax.swing.GroupLayout(addComplexEventPanel);
        addComplexEventPanel.setLayout(addComplexEventPanelLayout);
        addComplexEventPanelLayout.setHorizontalGroup(
            addComplexEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 768, Short.MAX_VALUE)
        );
        addComplexEventPanelLayout.setVerticalGroup(
            addComplexEventPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 447, Short.MAX_VALUE)
        );

        addEventTabPanel.addTab("Complex Event", addComplexEventPanel);

        javax.swing.GroupLayout addPanelLayout = new javax.swing.GroupLayout(addPanel);
        addPanel.setLayout(addPanelLayout);
        addPanelLayout.setHorizontalGroup(
            addPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(addEventTabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 773, Short.MAX_VALUE)
        );
        addPanelLayout.setVerticalGroup(
            addPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(addEventTabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 773, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(addPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 498, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 11, Short.MAX_VALUE)
                    .addComponent(addPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 12, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void serverListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_serverListValueChanged
        if (evt.getValueIsAdjusting() == false) {
            logListModel.clear();
            for (int i : serverList.getSelectedIndices()) {
                Server server = (Server) serverListModel.get(i);
                for (String log : server.getLogs()) {
                    logListModel.addElement(server.getName() + " - " + log);
                }
            }
        }
}//GEN-LAST:event_serverListValueChanged

    private void logListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_logListValueChanged
        if (evt.getValueIsAdjusting() == false) {
        }
}//GEN-LAST:event_logListValueChanged

    private void saveChangesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveChangesButtonActionPerformed
        ArrayList<Server> newEventServerList = new ArrayList<Server>();
        ArrayList<String> newEventLogList = new ArrayList<String>();
        for (int i : serverList.getSelectedIndices()) {
            Server serverFromList = (Server) serverListModel.get(i);
            for (int j : logList.getSelectedIndices()) {
                String log = (String) logListModel.getElementAt(j);
                String serverName = serverFromList.getName();
                String delete = serverName + " - ";
                log = log.replace(delete, "");
                newEventLogList.add(log);
            }
            Server server = new Server(serverFromList.getName(), serverFromList.getIp(), newEventLogList);
            newEventServerList.add(server);
        }
        //RegexPlugin event = new RegexPlugin(nameTextField.getText(), newEventServerList, regexTextField.getText());
        event.setName(nameTextField.getText());
        event.setServerList(newEventServerList);
        ((RegexPlugin) event).setRegex(regexTextField.getText());

        core.saveEventList();
        setVisible(false);
}//GEN-LAST:event_saveChangesButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel addComplexEventPanel;
    private javax.swing.JTabbedPane addEventTabPanel;
    private javax.swing.JPanel addPanel;
    private javax.swing.JPanel addSimpleEventPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList logList;
    private javax.swing.JLabel logListLabel;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLabel regexLabel;
    private javax.swing.JTextField regexTextField;
    private javax.swing.JButton saveChangesButton;
    private javax.swing.JList serverList;
    private javax.swing.JLabel serverListLabel;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the event
     */
    public Plugin getEvent() {
        return event;
    }

    /**
     * @param event the event to set
     */
    public void setEvent(Plugin event) {
        this.event = event;
        nameTextField.setText(event.getName());
        String command = event.getCommandStr();
        String regex = command.replaceAll("grep ", "");
        regexTextField.setText(regex);

        int indices[];
        int size = 0;
        ArrayList<Server> selectedServers = new ArrayList<Server>();
        for (Server eventServer : event.getServerList()) {
            Enumeration e = serverListModel.elements();
            while (e.hasMoreElements()) {
                Server serverFromList = (Server) e.nextElement();
                if (eventServer.toString().equals(serverFromList.toString())) {
                    ++size;
                    selectedServers.add(serverFromList);
                }
            }
        }
        indices = new int[size];
        for (int i = 0; i < size; ++i) {
            indices[i] = serverListModel.indexOf(selectedServers.get(i));
        }
        serverList.setSelectedIndices(indices);

        size = 0;
        ArrayList<String> selectedLogs = new ArrayList<String>();
        for (Server eventServer : event.getServerList()) {
            for (String log : eventServer.getLogs()) {
                log = eventServer.getName() + " - " + log;
                Enumeration e = logListModel.elements();
                while (e.hasMoreElements()) {
                    String logFromList = (String) e.nextElement();
                    if (log.equals(logFromList)) {
                        ++size;
                        selectedLogs.add(logFromList);
                    }
                }
            }
        }
        indices = new int[size];
        for (int i = 0; i < size; ++i) {
            indices[i] = logListModel.indexOf(selectedLogs.get(i));
        }
        logList.setSelectedIndices(indices);
    }
}
